#!/usr/bin/python

"""

Usage:

Should print a matrix file like this:

CYANO10001	1	0	0	0	1	1	1	1	0
CYANO10002	0	0	0	0	0	1	1	1	1
CYANO10003	1	1	1	1	1	0	0	0	0
.
.
.
TO BE CONTINUED. NOT DONE YET.

"""

import re
import sys

def formatoutput(ortholist):
    toprint = '\t'.join(ortholist)
    print toprint

ortholistfile = open(sys.argv[1], "rU")
orthos = ortholistfile.readlines()

biopfile = open(sys.argv[2], "rU")
biops = biopfile.readlines()

orthogroupsfile = open(sys.argv[3], "rU")
groups = orthogroups.readlines()

groupsdict = {}

for g in groups:
    group = g.split(' ')
    groupdict[group[0]] = group[1:] #dictionary of ortholog groups

ortholist = []

finalmatrix = []

for b in biops:
    bid = b.strip()
    for o in orthos:
        tmpcount = 0
        ortho = o.strip()
        tmplist = groupdict[ortho]
        tmpgrplist = []
        for m in tmplist:
            tstr = m.split('|')
            tmpgrplist.append((tstr[0], tstr[1]))
        for x in tmpgrplist:    
            if bid in x[0]:
                tmpcount = 1
            else:
                tmpcount = 0
        ortholist.append(tmpcount,)
    finalmatrix.append(ortholist) #list of dicts


ortholistfile.close()
biopfile.close()
orthogroupsfile.close()
